package com.hopu.v1.controller.front.question;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopu.v1.pojo.common.R;
import com.hopu.v1.pojo.question.QuestionsBanks;
import com.hopu.v1.service.front.question.FQuestionsBanksService;
import com.hopu.v1.vo.QuestionBanksVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 题库管理
 */
@RestController
@RequestMapping("/questionBanks")
public class FQuestionBanksController {
    @Autowired
    private FQuestionsBanksService FQuestionsBanksService;

    //获取指定的id题库
    @GetMapping("/get/{id}")
    public R getQuestionBanks(@PathVariable("id") Integer id){
        return R.ok(FQuestionsBanksService.getOne(new LambdaQueryWrapper<QuestionsBanks>().eq(QuestionsBanks::getId,id)));
    }

    //题库列表
    @GetMapping("/list")
    public R getQuestionBanksList(Page<QuestionsBanks> page,String name,Integer subjectId){
        LambdaQueryWrapper<QuestionsBanks> questionsBanksLambdaQueryWrapper = new LambdaQueryWrapper<>();
        questionsBanksLambdaQueryWrapper.eq(QuestionsBanks::getStatus,1);
        if (name != null){
            questionsBanksLambdaQueryWrapper.like(QuestionsBanks::getName,name);
        }
        if (subjectId != null){
            questionsBanksLambdaQueryWrapper.eq(QuestionsBanks::getSubjectId,subjectId);
        }
        return R.ok(FQuestionsBanksService.page(page, questionsBanksLambdaQueryWrapper));
    }

    //题库列表补充版，返回QuestionVo
    @GetMapping("/getList")
    public R getQuestionBanks(Page<QuestionsBanks> page,String name,Integer subjectId){
        List<QuestionBanksVo> questionBanks = FQuestionsBanksService.getQuestionBanks(page, name, subjectId);
        return R.ok(questionBanks);
    }

    //题库添加
    @PostMapping("/add")
    public R addQuestionBanks(@RequestBody QuestionsBanks questionsBanks){
        questionsBanks.setStatus(0);
        FQuestionsBanksService.save(questionsBanks);
        if (questionsBanks.getId() != null){
            return R.ok("添加成功");
        }else {
            return R.err("添加失败");
        }
    }
}
